<template>
  <div>
    <x-Table
      :loading="loading"
      :table-data="tableData"
      :options-data="options"
      :pagination="pagination"
      :rule-form="ruleForms"
      @handleAdd="handleAdd"
      @handlePaginationCurrentChange="handlePaginationCurrentChange"
      @handlePaginationSizeChange="handlePaginationSizeChange"
      @searchForm="searchForm"
      @resetForm="resetForm"
    >
    </x-Table>

  </div>
</template>

<script>
let _this;
export default {
  props:{
   ids:{
     type:Array,
     default:Array
   }
  },
  data() {
    return {
      searchParams: {},//搜索对象
      loading: true,
      row: null, //当前选择数据
      ruleForms: {
        major: {label: "专业", prop: "major_id", type: "select", select: null},
        name: {label: "姓名", prop: "name", type: "text"},
        sex: {label: "性别", prop: "sex", type: "select", select: [{label: '男', value: '男'}, {label: '女', value: '女'},]},
        idcard: {label: "身份证", prop: "idcard", type: "text"},
        phone: {label: "电话号", prop: "phone", type: "text"},
        student_number: {label: "学号", prop: "student_number", type: "text"},
        exam_number: {label: "准考证号", prop: "exam_number", type: "text"},
        enter_batch: {label: "入学批次", prop: "enter_batch", type: "text"},
        enter_type: {
          label: "入学类型",
          prop: "enter_type",
          type: "select",
          select: [{label: '春招', value: '春'}, {label: '秋招', value: '秋'},]
        },
      },
      pagination: {
        currentPage: 1,
        pageSize: 20,
        pageSizes: [10, 20, 50, 100],
      }, //分页参数
      otherParam: {
        halfCheckedKeys: [],
      }, //如果有其他情况会拼接这个参数
      fapi: {},
      formParam: {},
      dialogVisible: false,
      alertTitle: "添加",
      tableData: [],
      options: {
        buttonEdit: '编辑',
        buttonAdd: '添加',
        fields: {
          id: {label: "ID", prop: "id", width: "80", order: true},
          major_id: {label: "专业名称", prop: "majors", type: 'two', options: {obj: 'majors', objKey: 'title'}},
          name: {label: "姓名", prop: "name"},
          image: {
            label: '头像',
            prop: 'heard',
            type: 'image',
            sing: ',',
            style: {width: '30px', height: '30px'},
            preview: function (scope, item, index) {
              console.log(scope)
              console.log(item)
              console.log(index)
            },
          },
          sex: {
            label: "性别",
            prop: "sex",
            type: 'tag',
            options: [{label: '男', attr: 'primary'}, {label: '女', attr: 'success'}]
          },
          adress: {label: "地址", prop: "adress"},
          postal_code: {label: "邮政编码", prop: "postal_code"},
          idcard: {label: "身份证", prop: "idcard"},
          phone: {label: "电话", prop: "phone"},
          exam_number: {label: "准考证号", prop: "exam_number"},
          enter_batch: {label: "入学批次", prop: "enter_batch"},
          enter_type: {label: "入学类型", prop: "enter_type"},
          student_number: {label: "学号", prop: "student_number"},
          birthday: {label: "生日", prop: "birthday"},
          political: {label: "政治面貌", prop: "political"},
          graduated_school: {label: "毕业学校", prop: "graduated_school"},
          create_time: {label: "创建时间", prop: "create_time"},
        },
      },
    };
  },
  methods: {
    handlePaginationCurrentChange: function (val) {
      _this.loading = true;
      _this.pagination["currentPage"] = val;
      _this.getDateList(_this.searchParams);
    },

    handlePaginationSizeChange: function (val) {
      _this.loading = true;
      _this.pagination["pageSize"] = val;
      _this.getDateList(_this.searchParams);
    },

    /**
     * 获取所有数据
     */
    getDateList: function (val) {
      _this.$store
        .dispatch("office/getIdsFormUser", val?{
          ids:_this.ids,
          major_id:val.major_id,
          name: val.name,
          sex: val.sex,
          idcard: val.idcard,
          phone: val.phone,
          student_number: val.student_number,
          exam_number: val.exam_number,
          enter_batch: val.enter_batch,
          enter_type: val.enter_type,
          page: _this.pagination["currentPage"],
          pageSize: _this.pagination["pageSize"],
        }:{
          ids:_this.ids,
          page: _this.pagination["currentPage"],
          pageSize: _this.pagination["pageSize"],
        })
        .then(() => {
          _this.loading = false;
          let data = this.$store.state.office.studentList;
          this.tableData = data.data;
          _this.pagination["currentPage"] = data["current_page"];
          _this.pagination["total"] = data["total"];
        });
    },


    /**
     * 查询所有专业数据
     */
    getMajorList: function () {
      _this.$store
        .dispatch("office/getMajorList", {
          pageSize: 1000,
        })
        .then(() => {
          let data = this.$store.state.office.majorList;
          let param = [];
          data.data.forEach((item, index) => {
            let obj = {
              id: item.id,
              label: item.level + ' => ' + item.title,
              value: item.id
            }
            param.push(obj);
          })
          this.ruleForms['major']['select'] = param
        });
    },




    /**
     * 表单提交回调
     */
    onSubmit: function (val) {

    },

    resetForm: function () {
      _this.searchParams = {};
    },

    /**
     * 搜索角色标题
     */
    searchForm: function (val) {
      _this.searchParams = val;
      _this.loading = true;
      _this.getDateList(val);
    },
    search(val) {
      _this.$store
        .dispatch("office/searchStudent", {
          page: _this.pagination["currentPage"],
          pageSize: _this.pagination["pageSize"],
          name: val.name,
          sex: val.sex,
          idcard: val.idcard,
          phone: val.phone,
          student_number: val.student_number,
          exam_number: val.exam_number,
          enter_batch: val.enter_batch,
          enter_type: val.enter_type,
        })
        .then(() => {
          _this.loading = false;
          let data = this.$store.state.office.studentList;
          if (data) {
            this.tableData = data.data;
            _this.pagination["currentPage"] = data["current_page"];
            _this.pagination["total"] = data["total"];
          }
        });
    },
    /**
     * 更新角色
     */
    updateDate: function (param) {
      _this.$store
        .dispatch("office/updateStudent", {
          id: _this.row["id"],
          major_id: param["major_id"],
          heard: param["heard"],
          name: param["name"],
          sex: param["sex"],
          adress: param["adress"],
          postal_code: param["postal_code"],
          idcard: param["idcard"],
          phone: param["phone"],
          exam_number: param["exam_number"],
          pwd: param["pwd"],
          enter_batch: param["enter_batch"],
          enter_type: param["enter_type"],
          student_number: param["student_number"],
          birthday: param["birthday"],
          political: param["political"],
          graduated_school: param["graduated_school"],
        })
        .then((res) => {
          const {data, msg} = res;
          if (data) {
            _this.getDateList();
            _this.$message.success(msg);
            return;
          }
          return _this.$message.error(msg);
        });
    },

    /**
     * 删除角色
     */
    deleteDate: function (val) {
      _this.$store
        .dispatch("office/deleteStudent", {
          id: val,
        })
        .then((res) => {
          const {data, msg} = res;
          if (data) {
            _this.getDateList();
            _this.$message.success(msg);
            return;
          }
          return _this.$message.error(msg);
        });
    },

    /**
     *  表单导航栏添加点击
     */
    handleAdd: function () {
      _this.alertTitle = "添加";
    },
  },
  created() {
    _this = this;
  },
  mounted() {
    this.getDateList();
    this.getMajorList();
  },
};
</script>

<style scoped></style>
